import Vue from "vue";
import VueRouter from "vue-router"
Vue.use(VueRouter)

// 懒加载导入组件
const Login = ()=> import("@/pages/Login")
const Home = ()=> import("@/pages/Home")
const Register = ()=> import("@/pages/Register")
const Search = ()=> import("@/pages/Search")
const Test = ()=> import("@/pages/Test")

// 创建一个增强方法
function enhance(source,type){
    // 备份 VueRouter.prototype[type]  方法
    const orgin = source.prototype[type]
    // 增强方法
    source.prototype[type] = function(location,okCallback,errorCallback){
        if (okCallback || errorCallback) {
            orgin.call(this,location,okCallback,errorCallback)
        } else {
            orgin.call(this,location).catch(()=>{})
        }
    }
}
//增强 VueRouter 的 push 方法
enhance(VueRouter,"push")
//增强 VueRouter 的 replace 方法
enhance(VueRouter,"replace");

export default new VueRouter({
    mode:"history",
    routes:[
        {
            path: "/",
            component: Home
        },
        {
            path: "/login",
            component: Login,
        },
        {
            path: "/search",
            component: Search
        },
        {
            path: "/register",
            component: Register
        },
        {
            path: "/test",
            component: Test
        }
    ]
})

